package com.zzyy.study;

interface Dog
{
    public void myRun();
}

class Demu implements Dog
{
    @Override
    public void myRun()
    {
        System.out.println("---我是德牧");
    }
}
class Teddy implements Dog
{
    @Override
    public void myRun()
    {
        System.out.println("---我是泰迪");
    }
}

class Factory
{
    private Factory() {}

    //静态工厂，写死，给什么出什么
    public static Dog getInstance(String dogType)
    {
        if("Demu".equalsIgnoreCase(dogType)) {
            return new Demu();
        } else if ("Teddy".equalsIgnoreCase(dogType)) {
            return new Teddy();
        }else{
            return null;
        }
    }

    //动态工厂，写活，通过发射自动生成
    public static <T> T getInstanceByReflect(String fullClassName)
    {
        try
        {
            Object object = Class.forName(fullClassName).getDeclaredConstructor().newInstance();
            if(object instanceof Dog)
            {
                return (T)object;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
/**
 * @auther zzyy
 * @create 2024-03-25 23:13
 *
 * 反射+工厂设计模式
 */
public class T2
{
    public static void main(String[] args)
    {
        Dog myDog = Factory.getInstance("Demu");
        myDog.myRun();

        Dog myDog2 = Factory.getInstanceByReflect("com.zzyy.study.Teddy");
        myDog2.myRun();
    }
}
